<template>
	<view class="content">
		<view class="form-container">
			<view class="form-card">
				<view class="form-header">
					<text class="form-title">发表评论</text>
					<view class="decor-line"></view>
				</view>

				<view class="cu-form-group">
					<view class="input-header">
						<view class="cuIcon-edit" style="color: #667eea;font-size: 40rpx;"></view>
						<text class="input-label">评论内容</text>
					</view>
					<textarea class="comment-input" v-model="ruleForm.content" placeholder="请输入您的宝贵意见..."
						placeholder-class="placeholder-style" :maxlength="200" auto-height></textarea>
					<text class="word-count">{{ruleForm.content.length}}/200</text>
				</view>

				<view class="submit-btn" @tap="onSubmitTap">
					<text class="btn-text">提交评论</text>
					<view class="cuIcon-check"></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import wPicker from "@/components/w-picker/w-picker.vue";

	export default {
		data() {
			return {
				ruleForm: {
					refid: '',
					userid: '',
					nickname: '',
					content: '',
					reply: '',
				},
				// 登陆用户信息
				user: {},
				ro: {
					refid: false,
					userid: false,
					nickname: false,
					content: false,
					reply: false,
				},
				tzId: null,
				plType: ''
			}
		},
		components: {
			wPicker
		},
		computed: {},
		async onLoad(options) {
			// let table = uni.getStorageSync("nowTable");
			// // 获取用户信息
			// let res = await this.$api.session(table);
			// this.user = res.data;

			console.log('options',options)

			// 如果有登陆，获取登陆后保存的userid
			this.ruleForm.userid = uni.getStorageSync("userInfo").userid
			this.tzId = options.tzId
			this.plType = options.plType
			if (options.refid) {
				// 如果上一级页面传递了refid，获取改refid数据信息
				this.ruleForm.refid = options.refid;
				this.ruleForm.nickname = uni.getStorageSync("nickname");
			}
			// 如果是更新操作
			if (options.id) {
				this.ruleForm.id = options.id;
				// 获取信息
				res = await this.$api.info(`comment`, this.ruleForm.id);
				this.ruleForm = res.data;
			}
			// 跨表
			if (options.cross) {
				var obj = uni.getStorageSync('crossObj');
				for (var o in obj) {
					if (o == 'refid') {
						this.ruleForm.refid = obj[o];
						this.ro.refid = true;
						continue;
					}
					if (o == 'userid') {
						this.ruleForm.userid = obj[o];
						this.ro.userid = true;
						continue;
					}
					if (o == 'nickname') {
						this.ruleForm.nickname = obj[o];
						this.ro.nickname = true;
						continue;
					}
					if (o == 'content') {
						this.ruleForm.content = obj[o];
						this.ro.content = true;
						continue;
					}
					if (o == 'reply') {
						this.ruleForm.reply = obj[o];
						this.ro.reply = true;
						continue;
					}
				}
			}
			this.styleChange()
		},
		methods: {
			styleChange() {
				this.$nextTick(() => {
					// document.querySelectorAll('.app-update-pv .cu-form-group .uni-input-input').forEach(el=>{
					//   el.style.backgroundColor = this.addUpdateForm.input.content.backgroundColor
					// })
				})
			},
			getUUID() {
				return new Date().getTime();
			},
			async onSubmitTap() {
				const params = {
					userId: uni.getStorageSync('userInfo').userId,
					tzId: this.tzId,
					plType: this.plType,
					userName: uni.getStorageSync('userInfo').userName,
					userPhoto: uni.getStorageSync('userInfo').avatar,
					plContent: this.ruleForm.content
				}
				// if ((!this.ruleForm.refid)) {
				// 	this.$utils.msg(`关联表id不能为空`);
				// 	return
				// }
				// if ((!this.ruleForm.userid)) {
				// 	this.$utils.msg(`用户id不能为空`);
				// 	return
				// }
				if ((!this.ruleForm.content)) {
					this.$utils.msg(`评论内容不能为空`);
					return
				}
				if (this.ruleForm.id) {
					await this.$api.update(`comment`, this.ruleForm);
				} else {
					console.log('ruleForm', params)
					await this.$api.add(`comment`, params);
				}
				this.$utils.msgBack('提交成功');
			},
			optionsChange(e) {
				this.index = e.target.value
			},
			bindDateChange(e) {
				this.date = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			toggleTab(str) {
				this.$refs[str].show();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background: linear-gradient(15deg, #f8f9ff 0%, #f1f3fe 100%);
		min-height: 100vh;
		padding: 40rpx;
	}

	.form-container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
	}

	.form-card {
		background: rgba(255, 255, 255, 0.95);
		backdrop-filter: blur(20rpx);
		border-radius: 24rpx;
		padding: 48rpx;
		width: 90%;
		box-shadow: 0 8rpx 24rpx rgba(102, 126, 234, 0.15);
	}

	.form-header {
		margin-bottom: 48rpx;

		.form-title {
			font-size: 44rpx;
			font-weight: 600;
			color: #2d3748;
			position: relative;
			display: inline-block;
			padding-bottom: 16rpx;
		}

		.decor-line {
			height: 4rpx;
			width: 80rpx;
			background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
			border-radius: 2rpx;
		}
	}

	.cu-form-group {
		display: flex;
		flex-direction: column;
		margin-bottom: 48rpx;

		.input-header {
			display: flex;
			align-items: center;
			margin-bottom: 24rpx;

			.input-label {
				font-size: 32rpx;
				color: #667eea;
				margin-left: 16rpx;
				font-weight: 500;
			}
		}
	}

	.comment-input {
		width: 100%;
		min-height: 300rpx;
		padding: 32rpx;
		background: #f8f9ff;
		border-radius: 16rpx;
		font-size: 28rpx;
		color: #4a5568;
		border: 2rpx solid #e2e8f0;
		transition: all 0.3s ease;

		&:focus {
			border-color: #667eea;
			box-shadow: 0 0 16rpx rgba(102, 126, 234, 0.2);
		}
	}

	.placeholder-style {
		color: #a0aec0;
		font-size: 28rpx;
	}

	.word-count {
		display: block;
		text-align: right;
		font-size: 24rpx;
		color: #a0aec0;
		margin-top: 16rpx;
	}

	.submit-btn {
		background: linear-gradient(135deg, #667eea, #764ba2);
		border-radius: 60rpx;
		height: 96rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: white;
		font-size: 32rpx;
		font-weight: 500;
		box-shadow: 0 4rpx 16rpx rgba(102, 126, 234, 0.3);
		transition: all 0.2s ease;

		&:active {
			transform: scale(0.98);
			opacity: 0.9;
		}

		.btn-text {
			margin-right: 16rpx;
		}

		.cuIcon-check {
			font-size: 36rpx;
		}
	}
</style>